package com.cxx.utils;

import com.cxx.annotation.Autowired;
import com.cxx.annotation.Service;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 事务管理器
 *
 * @author Shuhang He
 * @version 1.0
 * @date 2021/3/14 21:36
 */
@Service
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtils;


    /**
     * 开启事务
     * @throws SQLException
     */
    public void begin() throws SQLException {
        Connection connection = connectionUtils.getCurrentThreadConn();
        connection.setAutoCommit(false);
    }

    /**
     * 事务提交
     * @throws SQLException
     */
    public void commit() throws SQLException {
        Connection connection = connectionUtils.getCurrentThreadConn();
        connection.commit();
    }

    /**
     * 事务回滚
     * @throws SQLException
     */
    public void rollback() throws SQLException {
        Connection connection = connectionUtils.getCurrentThreadConn();
        connection.rollback();
    }

    /**
     * 事务关闭
     * @throws SQLException
     */
    public void close() throws SQLException {
        Connection connection = connectionUtils.getCurrentThreadConn();
        connection.close();
    }
}
